package xio.ccf_201604;

import java.io.BufferedInputStream;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/*
  201604-4 游戏 100分
  Created by ywb47 on 2017/9/30.
 */
public class Game {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));
        int[][] direction = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
        Queue<Node> queue = new LinkedList<>();
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int tt = scanner.nextInt();
        int[][][] dangeroustime = new int[n][m][2];
        int[][][] markArray = new int[n][m][999]; //做标记 防止出现重复分支
        for (int i = 0; i < tt; i++) {
            int r = scanner.nextInt();
            int c = scanner.nextInt();
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            dangeroustime[r - 1][c - 1][0] = a;
            dangeroustime[r - 1][c - 1][1] = b;
        }
        queue.add(new Node(0, 0, 0));
        while (!queue.isEmpty()) {
            Node node = queue.poll();
            if (node.i == n - 1 && node.j == m - 1) {
                System.out.println(node.t);
                break;
            }
            for (int i = 0; i < 4; i++) {
                int nexti = node.i + direction[i][0];
                int nextj = node.j + direction[i][1];
                if (nexti >= 0 && nexti < n && nextj >= 0 && nextj < m && markArray[nexti][nextj][node.t ] == 0 && node.t + 1 < 300 && (node.t + 1 < dangeroustime[nexti][nextj][0] || node.t + 1 > dangeroustime[nexti][nextj][1])) {
                    queue.add(new Node(nexti, nextj, node.t + 1));
                    markArray[nexti][nextj][node.t] = 1; //做标记
                }
            }
        }
    }

    public static class Node {
        int i = 0, j = 0, t = 0;

        Node(int i1, int j1, int k1) {
            i = i1;
            j = j1;
            t = k1;
        }
    }

}
